﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DevExpress.Xpf.Core;
using ThanNhanTao.Server;
using ThanNhanTao.Server.BLL;
using ThanNhanTao.Server.Enum;
using ThanNhanTao.Server.Entities;
using ThanNhanTao.Report;
using DevExpress.Xpf.Printing;


namespace ThanNhanTao
{
    /// <summary>
    /// Interaction logic for frmThongKeTongBNChayThan.xaml
    /// </summary>
    public partial class frmThongKeBenhNhan : DXWindow
    {
        Dm_BenhVienBLL benhVienBLL;
        PhieuBenhNhanBll phieuBenhNhanBll;
        List<BenhNhanEntities> listBenhNhan;
        public frmThongKeBenhNhan()
        {
            InitializeComponent();
            listBenhNhan = new List<BenhNhanEntities>();
            benhVienBLL = new Dm_BenhVienBLL();
            phieuBenhNhanBll = new PhieuBenhNhanBll();
            cboKhuVuc.SelectedIndex = 0;
            cboGioiTinh.SelectedIndex = 0;
            cboNhomMau.SelectedIndex = 0;
            calendar.dpTuNgay.IsEnabled = false;
            calendar.rdbTheoQuy.IsEnabled = false;
            calendar.rdbTheoThang.IsChecked = true;
            BindNoiDKBD();
        }

        public void BindNoiDKBD()
        {
            List<BenhVienCustomEntities> listBenhVien = benhVienBLL.RetrieveAllBenhVienCustomEntities();
            cboNoiDKBD.ItemsSource = listBenhVien;
            cboNoiDKBD.EditValue = null;
        }

        private void btnDong_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnThongKe_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                byte trangThaiBenhNhan = EnumTrangThaiBenhNhan.TatCa;
                if (rdoDangDieuTri.IsChecked == true)
                    trangThaiBenhNhan = EnumTrangThaiBenhNhan.DangDieuTri;
                else if (rdoRaVien.IsChecked == true)
                    trangThaiBenhNhan = EnumTrangThaiBenhNhan.RaVien;
                bool? gioiTinh = null;
                if (cboGioiTinh.SelectedIndex == 1)
                    gioiTinh = true;
                else if (cboGioiTinh.SelectedIndex == 2)
                    gioiTinh = false;
                BenhVienCustomEntities bvEn = cboNoiDKBD.EditValue as BenhVienCustomEntities;
                listBenhNhan = phieuBenhNhanBll.ThongKeBenhNhan(calendar.StartDate, calendar.EndDate
                                    , bvEn != null ? bvEn.MaBV
                                        : (string.IsNullOrWhiteSpace(cboNoiDKBD.DisplayText)
                                        ? string.Empty : "-" + cboNoiDKBD.DisplayText)
                                    , cboKhuVuc.SelectedIndex == 0 ? string.Empty : cboKhuVuc.DisplayText
                                    , cboNhomMau.SelectedIndex == 0 ? string.Empty : cboNhomMau.DisplayText
                                    , trangThaiBenhNhan
                                    , gioiTinh
                                    , chkDoTuoi.IsChecked == true
                                    , Convert.ToInt32(spinEditDoTuoiTu.EditValue)
                                    , Convert.ToInt32(spinEditDoTuoiDen.EditValue)
                                    , cboHoSo.SelectedIndex
                                    , cboLoaiBenhNhan.SelectedIndex);
                rptThongKeBenhNhan rpt = new rptThongKeBenhNhan();
                rpt.DataSource = listBenhNhan;
                rpt.BindDetail(); //bind detail
                rpt.BindGroupFooter(listBenhNhan.Count);
                XtraReportPreviewModel model = new XtraReportPreviewModel(rpt);
                preview.Model = model;
                model.Report.CreateDocument(false);
            }
            catch (Exception)
            {
                DXMessageBox.Show("Thống kê không thành công", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void chkDoTuoi_Checked(object sender, RoutedEventArgs e)
        {
            spinEditDoTuoiTu.IsEnabled = true;
            spinEditDoTuoiDen.IsEnabled = true;
        }

        private void chkDoTuoi_Unchecked(object sender, RoutedEventArgs e)
        {
            spinEditDoTuoiTu.IsEnabled = false;
            spinEditDoTuoiDen.IsEnabled = false;
        }
    }
}
